<template>
  <div class="app-container">
    <template>
      <div class="hidden-md-and-up">
        <el-alert title="如若不适应手机端操作，建议使用电脑操作" type="warning" />
      </div>
    </template>
    <div class="table-container">
      <div style="padding-bottom:20px; display: flex;">
        <div style="width: 200px;">
          <el-input v-model="search" size="medium" placeholder="输入关键字搜索" />
        </div>
      </div>
      <el-table
        v-loading="paperListLoading"
        :data="paperList"
        element-loading-text="Loading"
        border
        fit
      >
        <el-table-column type="selection" align="center" width="55" />
        <!-- <el-table-column align="center" label="序号" width="55">
          <template slot-scope="scope">
            {{ scope.$index+1 }}
          </template>
        </el-table-column> -->
        <el-table-column align="center" label="负责人" min-width="120">
          <template slot-scope="scope">
            {{ scope.row.head }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="项目名称" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.projectName }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="学科类型" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.disciplineType }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="统计分类" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.classifi }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="联系地址" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.address }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="状态"
          :filters="[{ text: '草稿', value: '草稿' }, { text: '待院审核', value: '待院审核' }, { text: '被院级退回', value: '被院级退回' }, { text: '待校审核', value: '待校审核' }, { text: '被校级退回', value: '被校级退回' }, { text: '审核通过', value: '审核通过' }]"
          :filter-method="filterTag"
          filter-placement="bottom-end"
          min-width="120"
        >
          <template slot-scope="scope">
            <el-tag :type="scope.row.status | statusFilter">{{ scope.row.status }}</el-tag>
            <el-tag v-if="scope.row.status === '被院级退回' | scope.row.status === '被校级退回'" type="warning">{{ scope.row.reason }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="230">
          <template slot-scope="scope">
            <el-popover
              :ref="`popoverpass-${scope.$index}`"
              placement="top"
              width="160"
            >
              <p>确认通过吗？该操作不可撤销</p>
              <div style="text-align: right; margin: 0">
                <el-button size="mini" type="text" @click="scope._self.$refs[`popoverpass-${scope.$index}`].doClose()">取消</el-button>
                <el-button type="primary" size="mini" @click="thesis_pass(scope.$index, scope.row)">确定</el-button>
              </div>
            </el-popover>
            <el-button
              size="mini"
              @click="open_infoview_dialog(scope.row)"
            >
              查看
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="thesis_back(scope.row)"
            >
              退回
            </el-button>
            <el-button
              v-popover="`popoverpass-${scope.$index}`"
              size="mini"
              type="success"
            >
              通过
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          background
          :current-page="p1_page"
          :page-sizes="[1,2,3,10, 20, 30, 40]"
          :page-size="p1_size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="p1_total"
          @size-change="p1_sizechange"
          @current-change="p1_currentchange"
        />
        <!-- @size-change="handleSizeChange"
          @current-change="handleCurrentChange" -->
        <!-- 使用了size-change和current-change事件来处理页码大小和当前页变动时候触发的事件。 -->
      </div>
      <el-dialog title="查看信息" :visible.sync="infoview_dialog" width="75%">
        <el-collapse v-model="activeNames">
          <el-collapse-item name="1">
            <template slot="title">
              <div class="el-descriptions__title">科研平台信息</div>
            </template>
            <el-descriptions :column="2" size="medium" border :label-style="{width:'25%'}" :content-style="{width:'25%'}">
              <el-descriptions-item label="学科建设ID">
                {{ infoview1.constructionID }}
              </el-descriptions-item>
              <el-descriptions-item label="录入人">
                {{ infoview1.entered }}
              </el-descriptions-item>
              <el-descriptions-item label="学科负责人/带头人">
                {{ infoview1.head }}
              </el-descriptions-item>
              <el-descriptions-item label="录入日期">
                {{ infoview1.enteredDate }}
              </el-descriptions-item>
              <el-descriptions-item label="项目名称">
                {{ infoview1.projectName }}
              </el-descriptions-item>
              <el-descriptions-item label="统计分类">
                {{ infoview1.classifi }}
              </el-descriptions-item>
              <el-descriptions-item label="学科类型">
                {{ infoview1.disciplineType }}
              </el-descriptions-item>
              <el-descriptions-item label="联系地址">
                {{ infoview1.address }}
              </el-descriptions-item>
              <el-descriptions-item label="联系电话">
                {{ infoview1.phone }}
              </el-descriptions-item>
              <el-descriptions-item label="email">
                {{ infoview1.email }}
              </el-descriptions-item>
              <el-descriptions-item label="邮政编吗">
                {{ infoview1.zipcode }}
              </el-descriptions-item>
              <el-descriptions-item label="批准年度">
                {{ infoview1.approvalYear }}
              </el-descriptions-item>
              <el-descriptions-item label="批准批次">
                {{ infoview1.approvalBatch }}
              </el-descriptions-item>
              <el-descriptions-item label="状态">
                <el-tag :type="infoview1.status | statusFilter">{{ infoview1.status }}</el-tag>
              </el-descriptions-item>
              <el-descriptions-item label="未通过原因">
                {{ infoview1.reason }}
              </el-descriptions-item>
              <el-descriptions-item label="备注">
                {{ infoview1.notes }}
              </el-descriptions-item>
            </el-descriptions>
          </el-collapse-item>
        </el-collapse>
        <div slot="footer" class="dialog-footer">
          <el-button @click="infoview_dialog = false">关 闭</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { get_thesis_xiaoji, thesis_back_xiaoji, thesis_pass_xiaoji } from '@/api/discipline'
import { Message } from 'element-ui'
import { mapGetters } from 'vuex'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        草稿: 'info',
        待院审核: '',
        被院级退回: 'danger',
        待校审核: '',
        被校级退回: 'danger',
        审核通过: 'success'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      search: '',
      thesis_add_dialog: false,
      thesis_info_dialog: false,
      infoview_dialog: false,
      visible1: false,
      visible2: false,
      paperList: null,
      paperListLoading: false,
      p1_size: 10,
      p1_total: 0,
      p1_page: 1,
      back_post: {
        id: '',
        reason: ''
      },
      infoview1: {
        address: '',
        annex: '',
        approvalBatch: '',
        approvalYear: '',
        classifi: '',
        constructionID: '',
        disciplineType: '',
        email: '',
        entered: '',
        enteredDate: '',
        head: '',
        notes: '',
        phone: '',
        projectName: '',
        reason: '',
        status: '',
        zipcode: ''
      },
      activeNames: ['1']
    }
  },
  computed: {
    ...mapGetters([
      'uid',
      'roles',
      'name'
    ]),
    MyPaperPost() {
      return {
        name: this.name,
        pageNum: this.p1_page,
        pageSize: this.p1_size
      }
    }
  },
  created() {
    this.fetchPaperData()
  },
  methods: {
    fetchPaperData() {
      this.paperListLoading = true
      get_thesis_xiaoji(this.MyPaperPost).then(response => {
        if (response.code === 200) {
          this.paperList = response.data.list
          this.p1_total = response.data.total
          this.paperListLoading = false
        }
      })
    },
    p1_sizechange(newsize) {
      this.p1_size = newsize
      this.p1_page = 1
      this.fetchPaperData()
    },
    p1_currentchange(newpage) {
      this.p1_page = newpage
      this.fetchPaperData()
    },
    filterTag(value, row) {
      return row.status === value
    },
    thesis_back(row) {
      this.$prompt('请输入退回原因', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^.+$/,
        inputErrorMessage: '原因不能为空'
      }).then(({ value }) => {
        this.back_post.id = row.constructionID
        this.back_post.reason = value
        thesis_back_xiaoji(this.back_post).then(response => {
          if (response.code === 200) {
            Message.success('退回成功')
            this.fetchPaperData()
          }
        })
      }).catch(() => {
        Message.info('取消退回')
      })
    },
    thesis_pass(index, row) {
      this.$refs[`popoverpass-${index}`].doClose()
      thesis_pass_xiaoji(row.constructionID).then(response => {
        if (response.code === 200) {
          Message.success('通过成功')
          this.fetchPaperData()
        }
      })
    },
    open_infoview_dialog(row) {
      this.infoview_dialog = true
      this.infoview1 = JSON.parse(JSON.stringify(row))
    }
  }
}
</script>

<style lang="scss">
.el-form{
  margin-left: 40px;
  .el-input,.el-select{
    width: 300px;
  }
}
.el-tag + .el-tag {
  margin: 10px 10px;
}
.margin-top {
  margin-top: 50px;
}
@media screen and (max-width:600px) {
  .el-dialog{
    width: 100% !important;
  }
  .el-form{
    margin-left: 25px;
  }
}
</style>
